// 购物车
var appModule = angular.module('ShoppingCart', []);
appModule.controller('ShoppingCartCtrl', ['$scope', '$http', function($scope, $http) {
	// 将之前的base64码转为json
	$scope.params = eval('(' + decodeURIComponent(atob(getParameterByName('params'))) + ')');

	// 点击增加按钮
	$scope.decrease = function(Index, $event) {
		$scope.params.foodList[Index].foodNumber++;
		$scope.calcTotal();
	}

	// 点击减少按钮
	$scope.increase = function(Index, $event) {
		$scope.params.foodList[Index].foodNumber--;
		$scope.calcTotal();
		if($scope.params.foodList[Index].foodNumber == 0){
			$scope.params.foodList.splice(Index, 1);
		}
	}

	// 总价计算
	$scope.calcTotal = function() {
		$scope.total = 0;
		for (var i = 0; i < $scope.params.foodList.length; i++) {
			$scope.total = (parseFloat($scope.total) + $scope.params.foodList[i].foodNumber * parseFloat($scope.params.foodList[i].foodPrice)).toFixed(2);
		}
	}

	// 提交
	$scope.submit = function() {
		var totalAmount = 0;
		for (var i = 0; i < $scope.params.foodList.length; i++) {
			totalAmount += $scope.params.foodList[i].foodNumber;
		}
		if (totalAmount == 0) {
			alert('请至少选择一个餐点！');
		} else {
			location.href = 'order-food-confirm.html?params=' + btoa(encodeURIComponent(JSON.stringify($scope.params))) + '&fr=shoppingcart';
		}
	}

	//返回
	$scope.back = function() {
		location.href = 'order-foodinfo.html?params=' + btoa(encodeURIComponent(JSON.stringify($scope.params)));
	}

	// 浮点型计算
	$scope.parseFloat = parseFloat;
	$scope.calcTotal();
}]);